const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 物流基本信息-- 查询
router.post("/info", (req, res) => {
    const { values, pageConfig, doName } = req.body;
    const { page, pageSize } = pageConfig
    let sql = values === null ? "select * from moveinfo order by Id limit ?,?;select * from moveinfo"
        :
        "select * from moveinfo where traPro=? or traMode=? limit ?,?;select * from moveinfo where traPro=? or traMode=?"
    const data = values === null ? [(Number(page) - 1) * Number(pageSize), Number(pageSize)] :
        [values.fname, values.fmode, (Number(page) - 1) * Number(pageSize), Number(pageSize), values.fname, values.fmode]
    db.sqlQuery(sql, data, (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取信息失败",
                status: 203
            })
        } else {
            res.send({
                data: result[0],
                total: result[1].length,
                msg: "获取信息成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "物流信息查询", time.getData()])
        }
    })
})

// 物流基本信息-- 添加
router.post("/add", (req, res) => {
    let {
        traPro,
        traNum,
        traSpecs,
        traPerson,
        vehicle,
        traMode,
        traSendTime,
        traRecTime,
        deliveryPlace,
        recievePlace,
        distributor,
        distributorPer,
        distributorTel,
        insId, doName } = req.body
    deliveryPlace = deliveryPlace.join("-")
    recievePlace = recievePlace.join("-")
    let sql = `insert into moveinfo(traPro, traNum,
        traSpecs,
        traPerson,
        vehicle,
        traMode,
        traSendTime,
        traRecTime,
        deliveryPlace,
        recievePlace,
        distributor,
        distributorPer,
        distributorTel,
        insId) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
    const data = [traPro, traNum,
        traSpecs,
        traPerson,
        vehicle,
        traMode,
        traSendTime,
        traRecTime,
        deliveryPlace,
        recievePlace,
        distributor,
        distributorPer,
        distributorTel,
        insId]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            // if (err.errno === 1062) {
            //     console.log(err.sqlState)
            //     res.send({
            //         msg: "添加失败，该食品已存在，请重新添加！",
            //         status: 203
            //     })
            // } else if (err.errno === 1048) {
            //     res.send({
            //         msg: "添加失败,不能为空!",
            //         status: 203
            //     })
            // }
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "物流信息添加", time.getData()])
        }
    })
})

// 物流基本信息-- 编辑
router.post("/edit", (req, res) => {
    let {
        traPro,
        traNum,
        traSpecs,
        traPerson,
        vehicle,
        traMode,
        traSendTime,
        traRecTime,
        deliveryPlace,
        recievePlace,
        distributor,
        distributorPer,
        distributorTel, insId, Id, doName } = req.body
    deliveryPlace = deliveryPlace.join("-")
    recievePlace = recievePlace.join("-")
    let sql = `update moveinfo set
    traPro=?,
    traNum=?,
    traSpecs=?,
    traPerson=?,
    vehicle=?,
    traMode=?,
    traSendTime=?,
    traRecTime=?,
    deliveryPlace=?,
    recievePlace=?,
    distributor=?,
    distributorPer=?,
    distributorTel=?,
    insId=? where Id=?`
    const data = [
        traPro,
        traNum,
        traSpecs,
        traPerson,
        vehicle,
        traMode,
        traSendTime,
        traRecTime,
        deliveryPlace,
        recievePlace,
        distributor,
        distributorPer,
        distributorTel,
        insId, Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "物流信息编辑", time.getData()])
        }
    })

})

// 物流基本信息-- 删除
router.post("/delete", (req, res) => {
    const { Id, doName } = req.body
    let sql = "delete from moveinfo where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "物流信息删除", time.getData()])
})

module.exports = router